/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latesis.movil.ws;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.latesis.movil.dto.DTOConstantes;
import com.latesis.movil.dto.EnvioBackgroundRequestDTO;
import com.latesis.movil.dto.GPSTrackingRequestDTO;
import com.latesis.movil.dto.LoginRequestDTO;
import com.latesis.movil.dto.LoginResponseDTO;
import com.latesis.movil.dto.MarcacionRequestDTO;
import com.latesis.movil.dto.SincronizacionRequestDTO;
import com.latesis.movil.service.MovilService;
import com.latesis.movil.service.MovilServiceImpl;
import com.latesis.prueba.PruebaService;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.latesis.movil.dto.ConfirmacionServicioRequestDTO;
import com.latesis.util.GCMConstantes;
import java.io.IOException;

/**
 *
 * @author Diego
 */

@Controller
@RequestMapping("/movil")
public class MovilController {
    private static final Logger logger = Logger.getLogger(MovilController.class);
    
    @Resource(name="pruebaService")
    private PruebaService pruebaService;
    
    @Resource(name="movilService")
    private MovilService movilService;
    
    @RequestMapping(value="/{nombre}",method= RequestMethod.GET)
    public @ResponseBody String getSaludo(@PathVariable String nombre, ModelMap model){
        return "{mensaje2:'hola "+nombre+"'}";
    }
    
    @RequestMapping(value="/probarHibernate",method= RequestMethod.GET)
    public @ResponseBody String probarHibernate(ModelMap model){
        pruebaService.probarDAOs();
        return "probarHibernate...";
    }
    
    @RequestMapping(value="/",method= RequestMethod.GET)
    public @ResponseBody String prueba(ModelMap model){
        return pruebaService.saludar();        
    }                
    
    @RequestMapping(value="/login",method= RequestMethod.POST)
    public @ResponseBody String login(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        LoginRequestDTO request=gson.fromJson(requestData, LoginRequestDTO.class);
        
        String responseData = gson.toJson(movilService.login(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    }        
    
    
    @RequestMapping(value="/marcacion",method= RequestMethod.POST)
    public @ResponseBody String marcacion(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        MarcacionRequestDTO request=gson.fromJson(requestData, MarcacionRequestDTO.class);
        
        String responseData = gson.toJson(movilService.marcacion(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    }  
    
    @RequestMapping(value="/sincronizacion",method= RequestMethod.POST)
    public @ResponseBody String sincronizacion(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        SincronizacionRequestDTO request=gson.fromJson(requestData, SincronizacionRequestDTO.class);
        
        String responseData = gson.toJson(movilService.sincronizacion(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    }  
    
    @RequestMapping(value="/pendientes",method= RequestMethod.POST)
    public @ResponseBody String pendientes(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        EnvioBackgroundRequestDTO request=gson.fromJson(requestData, EnvioBackgroundRequestDTO.class);
        
        String responseData = gson.toJson(movilService.pendientes(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    }  
    
    @RequestMapping(value="/confirmacionServicio",method= RequestMethod.POST)
    public @ResponseBody String confirmacionServicio(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        ConfirmacionServicioRequestDTO request=gson.fromJson(requestData, ConfirmacionServicioRequestDTO.class);
        
        String responseData = gson.toJson(movilService.confirmacionServicio(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    }  
    
    @RequestMapping(value="/gpstracking",method= RequestMethod.POST)
    public @ResponseBody String gpstracking(@RequestBody String requestData){  
        logger.info(">>  request data: "+requestData);        
        Gson gson = new Gson();
        GPSTrackingRequestDTO request=gson.fromJson(requestData, GPSTrackingRequestDTO.class);
        
        String responseData = gson.toJson(movilService.gpstracking(request));
        logger.info(">>  response data: "+responseData);        
        return responseData;        
    } 
    
    @RequestMapping(value="/probarGCM/{regId}",method= RequestMethod.GET)
    public @ResponseBody String probarGCM(@PathVariable String regId, ModelMap model){
        
        String pushStatus;
        Result result = null;
 
        // GCM RedgId of Android device to send push notification
        try {        
            String userMessage = "hola mundo, soy el servidor";
            Sender sender = new Sender(GCMConstantes.GOOGLE_SERVER_KEY);
            Message message = new Message.Builder().timeToLive(30)
                .delayWhileIdle(true).addData(GCMConstantes.MESSAGE_KEY, userMessage).build();
            result = sender.send(message, regId, 1);
            pushStatus=result.toString();
        } catch (IOException ioe) {
            pushStatus="pushStatus...RegId required: " + ioe.toString();
        } catch (Exception e) {
            e.printStackTrace();
            pushStatus="pushStatus..."+e.toString();
        }          
            
        return "mensaje enviado por gcm a :"+regId+" ... pushStatus: "+pushStatus;
    }

    public void setPruebaService(PruebaService pruebaService) {
        this.pruebaService = pruebaService;
    }

    public void setMovilService(MovilService movilService) {
        this.movilService = movilService;
    }
    
}
